import { defineStore } from "pinia";
import { ref } from "vue";
import { reqAuthority } from "@/api/role";
import { reqSubjectInfo } from "@/api/subject";
import { scheduleName, scheduleCode, scheduleType } from "@/api/subject/type";
import { ElMessage } from "element-plus";
import type { SubjectRoleInfo } from "@/api/role/type";
import { reqRoleList } from "@/api/role";

// 定义在进入课题详情页时数据管理的仓库
const useSubjectStore = defineStore("SubjectStore", () => {
    // 定义页面的数量，防止重复
    const pageNum = ref<number>(0);
    // 当前访问的id
    const currentId = ref<number | string>(-1);
    const authority = ref<number>(-1);
    const currentInfo = ref<Subject>({
        id: -1,
        title: "暂未获取到标题",
        chairman: -1,
        remark: "",
        createTime: "",
        schedule: -1,
        tagArr: [],
        mamberArr: [],
        mamberIds: [],
        tagIds: [],
        status: -1,
    });
    // 当前的页面
    const currentPage = ref<string>("first");
    // 异步页面列表
    const asyncPageList = ref<Array<{ label: string; name: string; props: number; component: string }>>([]);
    // 成员列表
    const roleList = ref<Array<SubjectRoleInfo>>([]);
    // 进入课题时更新
    function init(subjectId: number | string) {
        currentId.value = subjectId;
        currentPage.value = "first";
        // 将页面删除
        asyncPageList.value = [];
        // 清除成员列表
        roleList.value = [];
        // 清除pageNum
        pageNum.value = 0;
        authority.value = -1;
        getAuthority();
        getSubjectInfo();
        return;
    }
    // 获取用户在指定课题内的权限
    async function getAuthority() {
        if (currentId.value === -1) {
            return;
        }
        const result = await reqAuthority(currentId.value);
        if (result.code !== 200 || typeof result.data === "string") {
            ElMessage.error(result.data as string);
            return;
        }
        authority.value = result.data.authoriyt;
        return;
    }
    // 获取课题相关的信息
    async function getSubjectInfo() {
        if (currentId.value === -1) {
            return;
        }
        const result = await reqSubjectInfo({
            subjectId: currentId.value,
        });
        if (result.code !== 200 || typeof result.data === "string") {
            ElMessage.error(result.data as string);
            return;
        }
        currentInfo.value = result.data;
        return;
    }
    // 新增上传日志的页面
    function addUploadLogPage() {
        const name = "log" + pageNum.value++;
        asyncPageList.value.push({
            component: "log",
            label: "上传日志",
            name,
            props: -1,
        });
        currentPage.value = name;
    }
    // 前往修改日志的页面
    function addEditLogPage(logId: number) {
        // 防止添加同一个日志的修改页面
        let pageName = "";
        asyncPageList.value.forEach((page) => {
            if (page.component === "log" && page.props !== -1 && page.props === logId) {
                pageName = page.name;
            }
        });
        if (pageName !== "") {
            currentPage.value = pageName;
            return;
        }
        const name = "log" + pageNum.value++;
        asyncPageList.value.push({
            component: "log",
            label: `修改编号为${logId}的日志`,
            name,
            props: logId,
        });
        currentPage.value = name;
    }
    // 新增上传任务的页面
    function addUploadWorkPage(schedule: number) {
        // 防止添加同一个进度的修改页面
        let pageName = "";
        asyncPageList.value.forEach((page) => {
            if (page.component === "schedule" && page.props !== -1 && page.props === schedule) {
                pageName = page.name;
            }
        });
        if (pageName !== "") {
            currentPage.value = pageName;
            return;
        }
        const name = "work" + pageNum.value++;
        asyncPageList.value.push({
            component: "schedule",
            label: scheduleName[scheduleCode[schedule] as scheduleType],
            name,
            props: schedule,
        });
        currentPage.value = name;
    }
    // 获取成员列表
    async function getRoleList() {
        const subjectId = currentId.value;
        const result = await reqRoleList(subjectId);
        if (result.code === 200) {
            roleList.value = result.data as SubjectRoleInfo[];
        } else {
            ElMessage.error(result.data as string);
        }
    }
    // 删除页面
    function removePage(name: string) {
        const index = asyncPageList.value.findIndex((page) => page.name === name);
        if (index !== -1) {
            asyncPageList.value.splice(index, 1);
        }
    }
    // 回到首页
    async function backSubjectHome() {
        currentPage.value = "first";
    }

    return {
        currentId,
        authority,
        currentInfo,
        currentPage,
        asyncPageList,
        roleList,
        addUploadLogPage,
        init,
        getSubjectInfo,
        getAuthority,
        backSubjectHome,
        addEditLogPage,
        removePage,
        addUploadWorkPage,
        getRoleList,
    };
});

export default useSubjectStore;
